package crm.model.teacherPC.net;

import java.util.ArrayList;
import java.util.Iterator;

import crm.model.CRMConst;
import crm.model.Setting;
import crm.model.teacherPC.Manager;



public class Aktualisator implements Runnable{
	private final ArrayList<StudentPC> students;

	Manager manager;
	public Thread thread;
	public Aktualisator(ArrayList<StudentPC> students, Manager manager) {
		this.students = students;
		this.manager=manager;
		start();
	}
	public void start() {
		(thread = new Thread(this)).start();
		System.out.println("Aktualisator started");
	}

	public void run() {
		while(CRMConst.connected){
			Iterator<StudentPC> iter = manager.getStudentIterator();
			synchronized (manager) {
				while (iter.hasNext()) {
					StudentPC student = iter.next();

					if (student.isConnected()) {
						student.refreshStatistic();
						student.refreshScreenShot();
						student.refreshWindows();
						student.refreshForegroundWindowName();
						manager.fireActulisingListener(student);
					} else {
						iter.remove();
						manager.fireStudentRemoved(student);
					}
				}
			}
			try {
				Thread.sleep(Setting.getAktualisatorTime());
			} catch (InterruptedException e) {

			}
		}
	}



}
